import { _decorator, Button, Component, director, EditBox, Label, Node, repeat } from 'cc';
import { NodeName } from '../../utils/common';
import { validPassword } from '../../utils/regex_func';
import { camelToSnakeCase, objectDropZeroLen, objectLength } from '../../utils/func';
const { ccclass, property } = _decorator;

@ccclass('StreamerCreateFormControl')
export class StreamerCreateFormControl extends Component {
    private submit_button: Node = null;
    private password_tooltip: Node = null;
    private password: Node = null;
    private re_password_tooltip: Node = null;
    private re_password: Node = null;

    private submit_form: object = {};

    protected onLoad(): void {
        this.submit_button = this.node.getChildByName(NodeName.StreamerCreateScene.Widget.SubmitWidget).getChildByName(NodeName.StreamerCreateScene.Button.SubmitButton);
        this.password_tooltip = this.node.getChildByName(NodeName.StreamerCreateScene.Widget.PasswordWidget).getChildByName(NodeName.StreamerCreateScene.Label.PasswordTooltipLabel);
        this.password = this.node.getChildByName(NodeName.StreamerCreateScene.Widget.PasswordWidget).getChildByName(NodeName.StreamerCreateScene.EditBox.PasswordEditBox);
        this.re_password_tooltip = this.node.getChildByName(NodeName.StreamerCreateScene.Widget.RePasswordWidget).getChildByName(NodeName.StreamerCreateScene.Label.RePasswordTooltipLabel);
        this.re_password = this.node.getChildByName(NodeName.StreamerCreateScene.Widget.RePasswordWidget).getChildByName(NodeName.StreamerCreateScene.EditBox.RePasswordEditBox);
        
        this.password.on(EditBox.EventType.TEXT_CHANGED, this.checkPasswordContent, this);
        this.re_password.on(EditBox.EventType.TEXT_CHANGED, this.checkRePasswordContent, this);

        this.submit_button.on(Button.EventType.CLICK, this.onSubmitButtonClick, this);
    }

    protected onDestroy(): void {
        if (this.submit_button !== null && this.submit_button !== undefined && this.submit_button.name.length) {
            this.submit_button.off(Button.EventType.CLICK, this.onSubmitButtonClick, this);
        }
    }

    checkPasswordContent() {
        /**
         * 检查首次输入的密码内容
         * 1. 获取输入的内容。
         * 2. 密码长度。
         * 3. 密码包含数字和字母。
        */
        if (this.password === null) {
            console.error('请先绑定密码组件');
        }
        if (this.password_tooltip === null) {
            console.error('请先绑定密码提示组件');
        }
        let password_content = this.password.getComponent(EditBox).string;
        let tooltip_comp = this.password_tooltip.getComponent(Label);
        
        // 需要先重置确认密码框的信息
        this.re_password.getComponent(EditBox).string = "";
        this.re_password_tooltip.getComponent(Label).string = "";
        
        if (password_content.length) {
            if (!validPassword(password_content)) {
                // 通过提示组件来提供提示信息
                tooltip_comp.string = "密码必须是8至12位的数字+字母";
            } else {
                tooltip_comp.string = "";
            }
        } else {
            tooltip_comp.string = "";
        }
        
    }

    checkRePasswordContent() {
        /**
         * 检查再次输入的密码内容
         * 1. 与首次输入是否一模一样。
        */
        if (this.re_password === null) {
            console.error('请先绑定确认密码组件');
        }
        if (this.re_password_tooltip === null) {
            console.error('请先绑定确认密码提示组件');
        }
        let tooltip_comp = this.re_password_tooltip.getComponent(Label);
        let password_content = this.password.getComponent(EditBox).string;
        let re_password_content = this.re_password.getComponent(EditBox).string;
        if (re_password_content.length) {
            if(password_content.length > 0 && password_content !== re_password_content) {
                tooltip_comp.string = "您前后输入的密码不一致";
            } else {
                tooltip_comp.string = "";
            }
        } else {
            tooltip_comp.string = "请先输入密码，然后再确认密码";
        }
    }

    onSubmitButtonClick() {
        this.node.getComponentsInChildren(EditBox).forEach((editbox) => {
            this.submit_form[camelToSnakeCase(editbox.node.name.replace('EditBox', ''))] = editbox.getComponent(EditBox).string;
        })
        this.node.getComponentsInChildren(Button).forEach((button) => {
            if (button.node.name === NodeName.StreamerCreateScene.Button.LivePlatformButton) {
                if (button.node.getChildByName('Label').getComponent(Label).string === '请选择直播平台信息') {
                    this.submit_form['live_platform'] = '';
                } else {
                    this.submit_form['live_platform'] = parseInt(button.node.getChildByName('Label').getComponent(Label).string.split('-')[0], 10);
                }
            }
        })
        console.log("二凤：", this.submit_form);
        if (objectLength(objectDropZeroLen(this.submit_form))) {
            // 向后端发送请求，并处理返回的结果
            // 跳转到新页面
            director.loadScene('StreamerCreateCompleteScene');
        } else {
            console.log('请输入数据');
        }
    }
}


